package com.csproj.siiat.base.common.dto;

import org.springframework.data.domain.Page;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseDTOV <T>{

    public static Boolean CODE_PARAM = false;

    private Boolean success;
    private Boolean unAuthorizedRequest;
    private T result;
    private String targetUrl;
    private Object error;


    public Object getResult() {
        return result;
    }

    public BaseDTOV setResult(T result) {
        this.result = result;
        return this;
    }

    public BaseDTOV setData(T result) {
        this.result = result;
        return this;
    }

    public String getTargetUrl() {
        return targetUrl;
    }

    public void setTargetUrl(String targetUrl) {
        this.targetUrl = targetUrl;
    }

    public Boolean getSuccess() {
        return success;
    }

    public BaseDTOV setSuccess(Boolean success) {
        this.success = success;
        return this;
    }

    public Object getError() {
        return error;
    }

    public void setError(Object error) {
        this.error = error;
    }

    public Boolean getUnAuthorizedRequest() {
        return unAuthorizedRequest;
    }

    public void setUnAuthorizedRequest(Boolean unAuthorizedRequest) {
        this.unAuthorizedRequest = unAuthorizedRequest;
    }






    public BaseDTOV setMessage(String message){
        if(message == null){
            return this;
        }
        if(this.error==null){
            this.error=new HashMap<String, String>();
        }
        ((Map<String, String>)this.error).put("message", message);
        return this;
    }

    public BaseDTOV setDetails(String details){
        if(details == null){
            return this;
        }
        if(this.error==null){
            this.error=new HashMap<String, String>();
        }
        ((Map<String, String>)this.error).put("details", details);
        return this;
    }

    public BaseDTOV(Boolean success, String message, T result){
        this.setSuccess(success);
        this.setUnAuthorizedRequest(true);
        this.setResult(result);
        this.setTargetUrl("");
        this.setMessage(message);
    }

    public BaseDTOV(Boolean success, Boolean unAuthorizedRequest, T result, String targetUrl, String message, String details){
        this.setSuccess(success);
        this.setUnAuthorizedRequest(unAuthorizedRequest);
        this.setResult(result);
        this.setTargetUrl(targetUrl);
        this.setMessage(message);
        this.setDetails(details);
    }

    public static BaseDTOV createBaseDTO(){
        BaseDTOV baseDTO2 = new BaseDTOV(true, true, null, "", null, null);
        return baseDTO2;
    }

    public static <T>BaseDTOV<T> createBaseDTO(T data){
        BaseDTOV baseDTO2 = new BaseDTOV(true, true, data, "", null, null);
        return baseDTO2;
    }

//    public static BaseDTOV createBaseDTO(Page page){
//        BaseDTOV baseDTO2 = new BaseDTOV(true, true, null, "", null,null);
//        baseDTO2.setTotalPage(page.getTotalPages())
//                .setTotalRow(page.getTotalElements());
////                .setFieldList(page.getContent());
//        return baseDTO2;
//    }

//    public static <T>BaseDTOV createBaseDTOByFieldList(List<T> fieldList){
//        BaseDTOV baseDTO2 = new BaseDTOV(true, true, null, "", null,null);
//        baseDTO2.setTotalPage(0)
//                .setTotalRow(0l)
//                .setFieldList(fieldList);
//        return baseDTO2;
//    }


}
